#include "pagehardwarestat.h"
#include "ui_pagehardwarestat.h"

PageHardWareStat::PageHardWareStat(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PageHardWareStat)
{
    ui->setupUi(this);
    setAutoFillBackground(true);  //设置自动填充
    QPalette palette = BKGCLR;
    setPalette(palette);
#ifndef RUN_IN_BOARD
    ui->Board_right->hide();
#else
    ui->Board_left->hide();ui->Board_right->hide();
#endif
    last_stat_1 = 0;
    last_stat_2 = 0;
    flush_interface_info();
    ui->Btn_ManualChangeBoard->setEnabled(false);
}

PageHardWareStat::~PageHardWareStat()
{
    delete ui;
}

void PageHardWareStat::flush_hardware_stat()
{
    ui->Btn_ManualChangeBoard->setEnabled(true);
#ifdef M1515
    if(avic_get_currentBoardId() == 1)
        ui->Board_left->show();
    else if(avic_get_currentBoardId() == 2)
        ui->Board_right->show();

    if(avic_get_mainBoardId() == 1)
    {
        ui->Master_left->setText("主");
        ui->Master_right->setText("从");
    }
    else if(avic_get_mainBoardId() == 2)
    {
        ui->Master_left->setText("从");
        ui->Master_right->setText("主");
    }

    int stat_1 = avic_get_boardStatus(0);
    if(stat_1 == 1)
    {
        ui->Board_stat_left->setText("正常");
        if(last_stat_1 == 0)
            GlobalConfig::addOperationInfo(QString("左板卡正常"));
    }
    else if(stat_1 == 0)
    {
        ui->Board_stat_left->setText("异常");
        if(last_stat_1 == 1)
            GlobalConfig::addOperationInfo(QString("左板卡异常"));
    }
    else
        ui->Board_stat_left->setText("错误");
    last_stat_1 = stat_1;

    int stat_2 = avic_get_boardStatus(1);
    if(stat_2 == 1)
    {
        ui->Board_stat_right->setText("正常");
        if(last_stat_2 == 0)
            GlobalConfig::addOperationInfo(QString("右板卡正常"));
    }
    else if(stat_2 == 0)
    {
        ui->Board_stat_right->setText("异常");
        if(last_stat_2 == 1)
            GlobalConfig::addOperationInfo(QString("右板卡异常"));
    }
    else
        ui->Board_stat_right->setText("错误");
    last_stat_2 = stat_2;
#endif
}

void PageHardWareStat::on_Btn_Return_clicked()
{
    emit returnToMaintMenu();
}

void PageHardWareStat::flush_interface_info()
{
    QString targetInterfaceName1 = "eth0"; // 你要获取IP地址的目标网口名称
    QString targetInterfaceName2 = "eth1"; // 你要获取IP地址的目标网口名称

    QList<QNetworkInterface> interfaceList = QNetworkInterface::allInterfaces();

    foreach (const QNetworkInterface &interface, interfaceList) {
        if (interface.name() == targetInterfaceName1) {
            QList<QNetworkAddressEntry> addressList = interface.addressEntries();
            foreach (const QNetworkAddressEntry &address, addressList) {
            qDebug() << "IP Address:" << address.ip().toString();
            if(address.ip().protocol() == QAbstractSocket::IPv4Protocol)
            {
#ifdef M1515
            if(avic_get_currentBoardId() == 1)
                ui->eth0_left->setText(address.ip().toString());
            else
                ui->eth0_right->setText(address.ip().toString());
#endif
            }
            }
        }
        if (interface.name() == targetInterfaceName2) {
            QList<QNetworkAddressEntry> addressList = interface.addressEntries();
            foreach (const QNetworkAddressEntry &address, addressList) {
            qDebug() << "IP Address:" << address.ip().toString();
            if(address.ip().protocol() == QAbstractSocket::IPv4Protocol)
            {
#ifdef M1515
            if(avic_get_currentBoardId() == 1)
                ui->eth1_left->setText(address.ip().toString());
            else
                ui->eth1_right->setText(address.ip().toString());
#endif
            }
            }
        }
    }
}

void PageHardWareStat::on_Btn_ManualChangeBoard_clicked()
{
    if(manual_change_cmd_)
        return;
    manual_change_cmd_ = true;
    emit signal_manual_change_board(true);
    QTimer::singleShot(10000,this,[=](){
        manual_change_cmd_ = false;
        emit signal_manual_change_board(false);
    });
}
